<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="/" title class="c-999 fsize14">首页</a>
        \
        <a href="/course" title class="c-999 fsize14">课程列表</a>
        \
        <span class="c-333 fsize14" v-text="courseInfo.title" />
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;overflow: hidden;">
          <section class="p-h-video-box" id="videoPlay">
            <img
              :src="courseInfo.cover"
              :alt="courseInfo.title"
              class="dis c-v-pic"
            />
          </section>
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24" v-text="courseInfo.title"></span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格：</span>
              <b
                class="c-yellow"
                style="font-size: 24px"
                v-if="courseInfo.price > 0"
                v-text="'￥' + courseInfo.price"
              ></b>
              <b
                class="c-yellow"
                style="font-size: 24px"
                v-else
                v-text="'￥' + courseInfo.price"
              ></b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span
                class="c-fff fsize14"
                v-text="'主讲： ' + teacherInfo.name"
              ></span>
            </section>
            <section class="c-attr-mt of">
              <span class="ml10 vam">
                <em class="icon18 scIcon"></em>
                <a class="c-fff vam" title="收藏" href="#">收藏</a>
              </span>
            </section>
            <section class="c-attr-mt">
              <a
                v-if="this.course.price===0||isBuyCourse"
                href="javascript:void(0);"
                title="立即观看"
                class="comm-btn c-btn-3"
                @click.stop="playVideo(0,videoId)"
                >立即观看</a
              >
              <a
                v-else
                href="javascript:void(0);"
                title="立即购买"
                class="comm-btn c-btn-3"
                @click.stop="playVideo(1,1)"
                >立即购买</a
              >
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol clearfix">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br />
                <h6 class="c-fff f-fM mt10" v-text="course.buyCount"></h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br />
                <h6 class="c-fff f-fM mt10" v-text="course.lessonNum"></h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br />
                <h6 class="c-fff f-fM mt10" v-text="course.viewCount"></h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"></div>
      </div>
      <!-- /课程封面介绍 -->
      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section
                      class="course-txt-body"
                      v-html="courseInfo.description"
                    >
                      <!-- <p>
                        Java的发展历史，可追溯到1990年。当时Sun&nbsp;Microsystem公司为了发展消费性电子产品而进行了一个名为Green的项目计划。该计划
                        负责人是James&nbsp;Gosling。起初他以C++来写一种内嵌式软件，可以放在烤面包机或PAD等小型电子消费设备里，使得机器更聪明，具有人工智
                        能。但他发现C++并不适合完成这类任务！因为C++常会有使系统失效的程序错误，尤其是内存管理，需要程序设计师记录并管理内存资源。这给设计师们造成
                        极大的负担，并可能产生许多bugs。&nbsp;
                        <br>为了解决所遇到的问题，Gosling决定要发展一种新的语言，来解决C++的潜在性危险问题，这个语言名叫Oak。Oak是一种可移植性语言，也就是一种平台独立语言，能够在各种芯片上运行。
                        <br>1994年，Oak技术日趋成熟，这时网络正开始蓬勃发展。Oak研发小组发现Oak很适合作为一种网络程序语言。因此发展了一个能与Oak配合的浏
                        览器--WebRunner，后更名为HotJava，它证明了Oak是一种能在网络上发展的程序语言。由于Oak商标已被注册，工程师们便想到以自己常
                        享用的咖啡(Java)来重新命名，并于Sun&nbsp;World&nbsp;95中被发表出来。
                      </p> -->
                    </section>
                  </div>
                </div>
                <!-- /课程介绍 -->
                <div class="mt50">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 文件目录 -->
                          <!-- <li class="lh-menu-stair">
                            <a href="javascript: void(0)" title="第一章" class="current-1">
                              <em class="lh-menu-i-1 icon18 mr10"></em>第一章
                            </a>
                            <ol class="lh-menu-ol" style="display: block;">
                              <li class="lh-menu-second ml30">
                                <a href="#" title>
                                  <span class="fr">
                                    <i class="free-icon vam mr10">免费试听</i>
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em>第一节
                                </a>
                              </li>
                              <li class="lh-menu-second ml30">
                                <a href="#" title class="current-2">
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em>第二节
                                </a>
                              </li>
                            </ol>
                          </li> -->
                          <li
                            class="lh-menu-stair"
                            v-for="chapter in chapterVoList"
                            :key="chapter.id"
                          >
                            <a
                              href="javascript:void(0)"
                              :title="chapter.title"
                              class="current-1"
                            >
                              <em class="lh-menu-i-1 icon18 mr10"></em>
                              {{ chapter.title }}
                            </a>
                            <ol class="lh-menu-ol" style="display: block">
                              <li
                                class="lh-menu-second ml30"
                                v-for="video in chapter.children"
                                :key="video.id"
                              >
                                <a
                                  v-if="isBuyCourse"
                                  href="javascript:void(0)"
                                  @click="playVideo(0, video.id)"
                                  title
                                >
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em
                                  >{{ video.title }}
                                </a>
                                <a
                                  v-else
                                  href="javascript:void(0)"
                                  @click="playVideo(video.isFree, video.id)"
                                  title
                                >
                                  <!-- <a :href="video.isFree==1?'/video/'+video.id:'javascript:void(0)'" title> -->
                                  <span class="fr" v-if="!isBuyCourse">
                                    <i class="free-icon vam mr10">{{
                                      video.isFree == 0
                                        ? "免费试听"
                                        : "购买后观看"
                                    }}</i>
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em
                                  >{{ video.title }}
                                </a>
                              </li>
                            </ol>
                          </li>
                        </ul>
                      </menu>
                    </div>
                  </section>
                </div>
                <!-- /课程大纲 -->
              </article>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto">
                  <li>
                    <div class="u-face">
                      <a href="#">
                        <img
                          :src="teacherInfo.avatar"
                          width="50"
                          height="50"
                          :alt="teacherInfo.name"
                        />
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a
                        class="c-333 fsize16 fl"
                        :href="'/teacher/' + teacherInfo.id"
                        v-text="teacherInfo.name"
                      ></a>
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999" v-text="teacherInfo.career" />
                    </section>
                  </li>
                </ul>
              </section>
            </div>
          </div>
        </aside>
        <div class="clear"></div>
      </div>
    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import course from '@/api/course'
import order from '@/api/order'
import cookie from "js-cookie"

export default {
  data() {
    return {
      courseId: null,
      videoId: '',
      customer: {},
      isBuyCourse: false,
      courseInfo: {
            id: '',
            teacherId: '',
            subjectId: '',
            subjectParentId: '',
            title: '',
            price: 0.00,
            lessonNum: 1,
            cover: '',
            description: null
        },
      course: {
            id: '',
            teacherId: '',
            subjectId: '',
            subjectParentId: '',
            title: '',
            price: 0.00,
            lessonNum: 0,
            cover: '',
            buyCount: '',
            viewCount: '',
            version: '',
            status: '',
            isDeleted: 0,
            gmtCreate: '',
            gmtModified: ''
        },
      teacherInfo: {
            id: '',
            name: '',
            intro: '',
            career: '',
            level: 0,
            avatar: '',
            sort: 0,
            isDeleted: 0,
            gmtCreate: '',
            gmtModified: ''
        },
        orderVo: {
          courseCover: "",
          courseId: "",
          courseTitle: "",
          memberId: "",
          mobile: "",
          nickname: "",
          payType: 0,
          teacherName: "",
          totalFee: 0
        },
      chapterVoList: null
    }
  },
  created() {
    this.courseId = this.$route.params.id
    this.getCourse()
    this.getCourseInfo()
    this.getTeacherInfoById()
    this.getChapterVo()
    this.init()
  },
  methods: {
    getCourseInfo() {
      course.getCourseInfo(this.courseId)
        .then(resp => {
          this.courseInfo = resp.data.data.courseInfo
        })
    },
     getCourse() {
      course.getCourse(this.courseId)
        .then(resp => {
          this.course = resp.data.data.items
          this.orderVo.courseId = this.course.id
          this.orderVo.courseCover = this.course.cover
          this.orderVo.courseTitle = this.course.title
          this.orderVo.totalFee = this.course.price
          if (this.customer) {
            course.getIsBuyCourse(this.orderVo.memberId,this.orderVo.courseId)
            .then(resp => {
              this.isBuyCourse = resp.data.success
            })
          }
        })
    },
    getTeacherInfoById() {
      course.getTeacherInfoById(this.courseId)
        .then(resp => {
          this.teacherInfo = resp.data.data.items
          this.orderVo.teacherName = this.teacherInfo.name
        })
    },
    getChapterVo() {
      course.getChapterVo(this.courseId)
        .then(resp => {
          this.chapterVoList = resp.data.data.items
          this.videoId = this.chapterVoList[0].children[0].id
        })
    },
    playVideo(isFree, videoId) {
      if (isFree == 0) {
        if (videoId !== '') {
          this.$router.push({ name: 'video-play', params: { play: videoId }})
        }
      } else {
        this.$confirm('此课程为付费课程, 是否购买?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          order.createOrder(this.orderVo)
            .then(resp => {
              if (!resp.data.success) {
                this.$message.error(resp.data.message);
                if (resp.data.data.orderNo) {
                  setTimeout(() => {
                    this.$router.push({ name: 'order-id', params: { id: resp.data.data.orderNo} })
                  },1000)
                }
              } else {
                this.$message({
                  message: '订单创建成功！',
                  type: 'success'
                })
                setTimeout(() => {
                  this.$router.push({ name: 'order-id', params: { id: resp.data.data.orderNo} })
                },1000)
              }
            })
        })
        .catch(() => {})
        // this.$router.push({ name: 'order-id', params: { id: videoId, course: this.course }})
      }
    },
    init() {
      this.token = cookie.get("user_token");
      const jsonStr = cookie.get("user_ucenter");
      if (jsonStr) {
        this.customer = JSON.parse(jsonStr);
        this.orderVo.memberId = this.customer.id
        this.orderVo.nickname = this.customer.nickname
        this.orderVo.mobile = this.customer.mobile
      } else {
        this.customer = undefined
      }
    }
  }
}
</script>